package com.leyou.item.service.impl.Attribute;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.impl.TbSpecGroupServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecificationService {
    @Autowired
    private TbSpecGroupServiceImpl specGroupService;
    public List<SpecGroupDTO> findSpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroupDTO> groupList = specGroupService.findSpecGroupByCategoryId(cid);
        // 查询分类下所有规格参数
        List<SpecParamDTO> params = specGroupService.findSpecParamByGroupId(null, cid, null);
        // 将规格参数按照groupId进行分组，得到每个group下的param的集合
        Map<Long, List<SpecParamDTO>> paramMap = params.stream()
                .collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        // 填写到group中
        for (SpecGroupDTO groupDTO : groupList) {
            groupDTO.setParams(paramMap.get(groupDTO.getId()));
        }
        return groupList;
    }
}
